/***********************************************************************
 * Dao Interface :  SysOperationDao.java
 * Author:  Bill Vison
 * Purpose: Defines the Hibernate Implement SysOperationDaoImpl
 ***********************************************************************/
package org.vsg.security.model.persistence.hibernate;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.dao.PaginationHibernateDaoSupport;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.security.model.domain.SysOperation;
import org.vsg.security.model.persistence.SysOperationDao;
import org.vsg.security.model.persistence.hibernate.proc.SysOperationByAllPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysOperationPaginationProc;

/**
 * @author Bill Vison
 */
public class SysOperationDaoImpl extends PaginationHibernateDaoSupport implements SysOperationDao {

	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysOperationDaoImpl.class);

	
	/**
	 *
	 * @param sysOperation
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysOperationDao#save(org.vsg.security.model.domain.SysOperation)
	 */
	@Override
	public void save(SysOperation sysOperation) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(sysOperation);
	}

	@Override
	public void saveAll(Collection<SysOperation> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
		getHibernateTemplate().saveOrUpdateAll(all);
		
	}   	
   	
	/**
	 *
	 * @param sysOperation
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysOperationDao#update(org.vsg.security.model.domain.SysOperation)
	 */
   	@Override
   	public void update(SysOperation sysOperation) throws DataAccessException {
   		// TODO Auto-generated method stub
		getHibernateTemplate().update(sysOperation);
	}   	


	/**
	 *
	 * @param sysOperation
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysOperationDao#delete(org.vsg.security.model.domain.SysOperation)
	 */
	@Override
	public void delete(SysOperation sysOperation) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(sysOperation);
	}

	@Override
	public void deleteAll(Collection<SysOperation> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().deleteAll(all);
	}
   	
   	
	/**
	 *
	 * @param pk , the primary key
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysOperationDao#load(java.io.Serializable)
	 */
	@Override
	public SysOperation load(Serializable pk) throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		SysOperation sysOperation = (SysOperation) getHibernateTemplate().load(SysOperation.class, pk);
		
		return sysOperation;	
		// End of user code	
	}
   
   
	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysOperationDao#pageSysOperation(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysOperation(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysOperationPaginationProc proc = new SysOperationPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code	
	}      	


	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysOperationDao#pageSysOperationByAll(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysOperationByAll(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysOperationByAllPaginationProc proc = new SysOperationByAllPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code		
	}


	/**
	 * @exception DataAccessException
	 */
	@Override
	public Collection<SysOperation> findByAll() throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");

		query.append("sysOperation ");
		query.append("from ").append(SysOperation.class.getName()).append(" as sysOperation ");
		query.append("where sysOperation.flagDeleted = 0 ");
		query.append("order by sysOperation.name");
		
		Collection params = new Vector();
		
		logger.info("query : " + query.toString());

		Iterator<SysOperation> iter = getHibernateTemplate().iterate(query.toString());
		
		Collection<SysOperation> result = new Vector<SysOperation>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
		// End of user code			
	}

	@Override
	public Collection<SysOperation> findByOperation(String operation)
			throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("operation ");
		query.append("from ").append(SysOperation.class.getName());
		query.append(" as operation ");
		query.append("where operation.operation=? ");
		
		Collection paramColl = new Vector();
		paramColl.add(operation);
		
		Iterator<SysOperation> iter = this.getHibernateTemplate().iterate(query.toString() , paramColl.toArray());
		
		Collection<SysOperation> all = new Vector<SysOperation>();
		
		SysOperation opera = null;
		while (iter.hasNext()) {
			opera = iter.next();
			all.add( opera );
		}
		return all;
	}

	
   
} 